package com.cloud.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.common.SessionManager;
import com.cloud.pojo.PhoneRelation;
import com.cloud.pojo.Salesman;
import com.cloud.service.SalesmanService;

/**
 *@Author: liu
 *@Date: Created on 2017年11月3日 下午4:26:02
 */
@Controller
@RequestMapping(value = "salesman")
public class SalesmanController {
	
	
	@Autowired
	private SalesmanService salesmanService;
	
	@RequestMapping("login")
	@ResponseBody
	public Map<String, Object> login(HttpServletRequest request,Salesman salesman){
		Map<String, Object> data = new HashMap<>();
		try {
			Salesman admin = salesmanService.selectSalesmanByUsername(salesman);
			if(admin == null){
				data.put("status", "001");
				data.put("msg", "账号不存在!");
				data.put("data", null);
			}else if(!admin.getPassword().equals(salesman.getPassword())){
				data.put("status", "002");
				data.put("msg", "密码不正确!");
				data.put("data", null);
			}else {
				request.getSession().setAttribute("id", admin.getId());
				request.getSession().setAttribute("username", admin.getUsername());
				request.getSession().setAttribute("salesman", admin);
				SessionManager.addUser(admin.getId(), request.getSession());
				data.put("status", "000");
				data.put("msg", "登录成功!");
				data.put("data", null);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return data;
	}

}
